package com.yc.scott.ui;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JTabbedPane;

import com.yc.scott.biz.EmpBiz;

import javax.swing.JLabel;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.Map;
import java.awt.event.ActionEvent;

public class MainWin {

	JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWin window = new MainWin();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWin() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 669, 542);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);
		
		JMenu mnNewMenu = new JMenu("基础数据管理");
		menuBar.add(mnNewMenu);
		
		JMenuItem mntmNewMenuItem = new JMenuItem("部门管理");
		
		mnNewMenu.add(mntmNewMenuItem);
		
		JMenuItem mntmNewMenuItem_1 = new JMenuItem("查询员工");
		
		mnNewMenu.add(mntmNewMenuItem_1);
		
		JMenu mnNewMenu_1 = new JMenu("人事管理");
		menuBar.add(mnNewMenu_1);
		
		JMenuItem mntmNewMenuItem_2 = new JMenuItem("员工入职");
		mntmNewMenuItem_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new EmpEditDlg().setVisible(true);
			}
		});
		mnNewMenu_1.add(mntmNewMenuItem_2);
		
		JMenuItem mntmNewMenuItem_3 = new JMenuItem("员工离职");
		mnNewMenu_1.add(mntmNewMenuItem_3);
		
		JMenuItem mntmNewMenuItem_5 = new JMenuItem("修改员工信息");
		mnNewMenu_1.add(mntmNewMenuItem_5);
		mntmNewMenuItem_5.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				new EmpEditDlg(true).setVisible(true);
			}
		});
		
		JMenu mnNewMenu_2 = new JMenu("帮助");
		menuBar.add(mnNewMenu_2);
		
		JMenuItem mntmNewMenuItem_4 = new JMenuItem("关于我们");
		mnNewMenu_2.add(mntmNewMenuItem_4);
		
		JPanel panel = new JPanel();
		panel.setPreferredSize(new Dimension(100, 10));
		frame.getContentPane().add(panel, BorderLayout.WEST);
		
		JButton button = new JButton("请假");
		panel.add(button);
		
		JButton btnNewButton = new JButton("考勤");
		panel.add(btnNewButton);
		
		JButton btnNewButton_1 = new JButton("入职");
		
		panel.add(btnNewButton_1);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon("E:\\bg.jpg"));
		tabbedPane.addTab("首页", null, lblNewLabel, null);
		
		//////////////////////////////////
		mntmNewMenuItem_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EmpDlg dialog = new EmpDlg();
				dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
				dialog.setVisible(true);
			}
		});
		
		mntmNewMenuItem.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addTab("部门管理", DeptPanel.class);
			}
		});
		
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				addTab("入职", AddEmpPanel.class);
			}
		});
		
		Map<String,Object> loginedUser = EmpBiz.getLoginedUser();
		if(loginedUser != null) {
			frame.setTitle("欢迎:" + loginedUser.get("ENAME"));
		}
	}
	
	
	JTabbedPane tabbedPane;
	@SuppressWarnings("deprecation")
	public void addTab(String title, Class<?> cls) {
		for (int i = 0; i < tabbedPane.getTabCount(); i++) {
			if(tabbedPane.getTitleAt(i).equals(title)) {
				// 查找 => 切换
				tabbedPane.setSelectedIndex(i);
				return;
			}
		}
		JPanel panel;
		try {
			panel = (JPanel) cls.newInstance();
			tabbedPane.addTab(title, null, panel, null);
			// 切换到当前新增页面
			tabbedPane.setSelectedIndex(tabbedPane.getTabCount()-1);
		} catch (InstantiationException | IllegalAccessException e) {
			throw new RuntimeException("创建控件失败!", e);
		}
		
	}
	
}
